package com.ycc.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycc.biz.exception.BusinessException;
import com.ycc.biz.mapper.BizAttachmentMapper;
import com.ycc.biz.pojo.entity.BizAttachment;
import com.ycc.biz.service.BizAttachmentService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 附件Service实现类
 */
@Service
public class BizAttachmentServiceImpl extends ServiceImpl<BizAttachmentMapper, BizAttachment> implements BizAttachmentService {

    // 附件上传路径
    private static final String UPLOAD_PATH = "uploads/file/";

    @Override
    public BizAttachment uploadFile(MultipartFile file) {
        // 上传文件到服务器
        if (file == null || file.isEmpty()) {
            throw new BusinessException(400, "上传文件不能为空");
        }

        // 获取文件名
        String originalFilename = file.getOriginalFilename();
        // 获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 生成新的文件名
        String fileName = UUID.randomUUID().toString() + suffix;

        // 创建上传目录
        File uploadDir = new File(UPLOAD_PATH);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        // 创建目标文件
        File targetFile = new File(uploadDir.getAbsolutePath() + File.separator + fileName);

        BizAttachment attachment = BizAttachment.instance();
        attachment.setOriginalFilename(originalFilename);
        attachment.setFilename(fileName);
        attachment.setFilePath(UPLOAD_PATH + fileName);
        attachment.setContentType(file.getContentType());
        attachment.setFileSize(file.getSize());
        save(attachment);

        try {
            // 保存文件
            file.transferTo(targetFile);
            // 返回文件访问路径
            return attachment;
        } catch (IOException e) {
            throw new BusinessException(500, "文件上传失败");
        }
    }

    @Override
    public BizAttachment getByBusiness(String businessId, String businessType) {
        LambdaQueryWrapper<BizAttachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizAttachment::getBusinessId, businessId)
                .eq(BizAttachment::getBusinessType, businessType)
                .last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    public List<BizAttachment> listByBusiness(String businessId, String businessType) {
        LambdaQueryWrapper<BizAttachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizAttachment::getBusinessId, businessId)
                .eq(BizAttachment::getBusinessType, businessType)
                .orderByDesc(BizAttachment::getCreateTime);
        return list(queryWrapper);
    }

    /**
     * 获取文件扩展名
     */
    private String getFileExtension(String filename) {
        if (filename == null || filename.lastIndexOf(".") == -1) {
            return "";
        }
        return filename.substring(filename.lastIndexOf("."));
    }
} 